package com.ruoyi.ewem.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ewem.domain.ProvinceCityDistrict;
import com.ruoyi.ewem.domain.Warehouse;
import com.ruoyi.ewem.feign.UserFeignClient;
import com.ruoyi.ewem.service.ProvinceCityDistrictService;
import com.ruoyi.ewem.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * @Author: Yanyanyu
 * @Date: 2024/7/29 15:55
 */
@RestController
@RequestMapping("warehouse")
public class WarehouseController extends BaseController {

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private ProvinceCityDistrictService provinceCityDistrictService;

    @Autowired
    private UserFeignClient userFeignClient;


    /**
     * 查询仓库列表
     */
    @RequiresPermissions("ewem:warehouse:list")
    @GetMapping("/list")
    public TableDataInfo list(Warehouse warehouse) {
        startPage();
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        return getDataTable(list);
    }

    /**
     * 出库
     */
    @PutMapping("/stockOut")
    public AjaxResult stockOut(@RequestBody Warehouse warehouse){
        return toAjax(warehouseService.stockOut(warehouse));
    }

    /**
     * 获取所有司机信息
     */
    @GetMapping("/getDriver/{warehouseId}")
    public AjaxResult getDrivers(@PathVariable Integer warehouseId){
        AjaxResult result = new AjaxResult();
        result.put("drivers",userFeignClient.getDrivers());
        List<Warehouse> warehouses = warehouseService.selectWarehouseById(warehouseId);
        result.put("warehouse",warehouses);
        return result;
    }

    /**
     * 导出仓库列表
     */
    @RequiresPermissions("ewem:warehouse:export")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Warehouse warehouse) {
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        ExcelUtil<Warehouse> util = new ExcelUtil<Warehouse>(Warehouse.class);
        util.exportExcel(response, list, "仓库数据");
    }

    /**
     * 获取仓库详细信息
     */
    @RequiresPermissions("ewem:warehouse:query")
    @GetMapping(value = { "/", "/{warehouseId}" })
    public AjaxResult getInfo(@PathVariable(value = "warehouseId",required = false) Integer warehouseId) {

        AjaxResult result = new AjaxResult();

        List<ProvinceCityDistrict> list = provinceCityDistrictService.list();

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都有默认值的
        treeNodeConfig.setChildrenKey("children");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setIdKey("value");
        // 最大递归深度
        treeNodeConfig.setDeep(3);

        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<ProvinceCityDistrict, String>build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getPid()+"");
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
//                    tree.putExtra("extraField", 666);
//                    tree.putExtra("other", new Object());
                });

        Warehouse warehouse = warehouseService.getById(warehouseId);
        result.put("treeNodes",treeNodes);
        result.put("data",warehouse);
        return result;
    }

    /**
     * 新增仓库
     */
    @RequiresPermissions("ewem:warehouse:add")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Warehouse warehouse) {
        warehouse.setCreateBy(SecurityUtils.getUsername());
        warehouse.setCreateTime(new Date());
        return toAjax(warehouseService.save(warehouse));
    }

    /**
     * 修改仓库
     */
    @RequiresPermissions("ewem:warehouse:edit")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Warehouse warehouse) {
        warehouse.setUpdateBy(SecurityUtils.getUsername());
        warehouse.setUpdateTime(new Date());
        return toAjax(warehouseService.updateById(warehouse));
    }

    /**
     * 删除仓库
     */
    @RequiresPermissions("ewem:warehouse:remove")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{warehouseIds}")
    public AjaxResult remove(@PathVariable Long[] warehouseIds) {
        return toAjax(warehouseService.removeBatchByIds(Arrays.asList(warehouseIds)));
    }

    /**
     * 报表统计
     */
    @GetMapping("/echarts")
    public AjaxResult echarts(){
        List<Warehouse> list=warehouseService.selectCounts();
        return success(list);
    }

}
